package cn.dujc.superkid.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import java.util.List;

import cn.dujc.superkid.R;
import cn.dujc.superkid.bean.VideoIndexResults;

public class CourseFragmentRightAdapter extends RecyclerView.Adapter<CourseFragmentRightAdapter.MyHolder> {
    private static final String TAG = "CourseFragmentLeftAdapt";
    List<VideoIndexResults.DataBean.KnowBean> know;
    private Context context;
    private OnRightItemClickListener onRightItemClickListener;

    public CourseFragmentRightAdapter(List<VideoIndexResults.DataBean.KnowBean> know, Context context, OnRightItemClickListener onRightItemClickListener) {
        this.know = know;
        this.context = context;
        this.onRightItemClickListener = onRightItemClickListener;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LinearLayout view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item_rv_course_fragment_right, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        final VideoIndexResults.DataBean.KnowBean knowBean = know.get(position);
        if (holder != null) {
            String k_name = knowBean.getK_name();
            holder.tvContentCourse.setText(k_name);
            final List<VideoIndexResults.DataBean.KnowBean.KnowlistBean> knowlist = knowBean.getKnowlist();
            final LayoutInflater mInflater = LayoutInflater.from(context);
            holder.flowLayout.setAdapter(new TagAdapter<VideoIndexResults.DataBean.KnowBean.KnowlistBean>(knowlist) {
                @Override
                public View getView(FlowLayout parent, final int position, final VideoIndexResults.DataBean.KnowBean.KnowlistBean knowlistBean) {
                    TextView tv = (TextView) mInflater.inflate(R.layout.tv_right_flow_layout,
                           holder.flowLayout , false);
                    tv.setText(knowlistBean.getTitle());
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.d(TAG, "onBindViewHolder呵呵: "+position);
                            onRightItemClickListener.onRightItemClickListener(knowlist.get(position));
                        }
                    });
                    return tv;
                }
            });

//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    onRightItemClickListener.onRightItemClickListener();
//                }
//            });
        }
    }

    @Override
    public int getItemCount() {
        return know.size();
    }

    public  class MyHolder extends RecyclerView.ViewHolder {
        TextView tvContentCourse;
        TagFlowLayout flowLayout;
        public MyHolder(View itemView) {
            super(itemView);
            tvContentCourse = itemView.findViewById(R.id.tv_content_course);
            flowLayout = itemView.findViewById(R.id.flow_layout);
        }
    }

    public interface OnRightItemClickListener {
        void onRightItemClickListener(VideoIndexResults.DataBean.KnowBean.KnowlistBean knowlistBean);
    }
}
